// components/audio/audio.js
var app = getApp()
var raduoState = "Stop"
// var rm = wx.getRecorderManager();
var plugin = requirePlugin("WechatSI")
let manager = null;

Component({
      /**
       * 组件的属性列表
       */
      properties: {

      },
      lifetimes:{
        ready(){
           manager = plugin.getRecordRecognitionManager()
           this.recordListener()
        }

      },

      /**
       * 组件的初始数据
       */
      data: {
       
        audio_view:'audio_view'
      },

      /**
       * 组件的方法列表
       */
      methods: {
        touchStart(e){
          if(raduoState !='Start'){
            this.setData({
              audio_view:'audio_viewafter'
            })
            wx.vibrateShort({})
            manager.start({duration:30000, lang: "zh_CN"})  
          } 
        },
        touchEnd(e){
          this.setData({
            audio_view:'audio_view'
          })
          manager.stop({})
          raduoState = "Stop" 
        },
        recordListener(){
          var that = this   
          manager.onStart = function(res) {
            console.log("成功开始录音识别", res)
            raduoState = "Start"
            console.log(Date.now())
        }
          manager.onStop = function(res) {          
            console.log(res)
            raduoState = "Stop"
            console.log("onStop")
            that.setData({
              audio_view:'audio_view'
            })
          
            that.triggerEvent("record",res.result)
            
        }
          manager.onRecognize = function(res) {
            console.log("current result", res.result)
        }
      
          manager.onError = function(res) {
            console.error("error msg", res.msg)
            wx.showToast({
              title: '未识别语音',
              icon:'error',
            })
            raduoState = "Error"
        }
        },
        
      }
})